#include "cfgame.h"
#include <string>
#include "SDL.h"
#include "mixer.h"
#include <cstdlib>

using namespace std;

extern string PLAYER1;
extern string PLAYER2;

// 0 Nada. 1 Gana 1. 2 Gana 2. 3 Empate
int  coll_table [13][13] =
{
 {0,0,2,2,2,2,2,0,0,0,0,0,0}, /* ST_DEPIE */
 {0,0,2,2,2,2,2,0,0,0,0,0,0}, /* ST_SALTO */
 {1,1,3,3,3,3,3,0,1,1,0,0,0}, /* ST_PUNYO */
 {1,1,3,3,3,3,3,0,1,1,0,0,0}, /* ST_PATA */
 {1,1,3,3,3,3,3,0,1,1,0,0,0}, /* ST_SPUNYO */
 {1,1,3,3,3,3,3,0,1,1,0,0,0}, /* ST_SPATA */
 {1,1,3,3,3,3,3,0,1,1,0,0,0}, /* ST_SPECIAL1 */
 {0,0,0,0,0,0,0,0,0,0,0,0,0}, /* ST_POSE */
 {0,0,2,2,2,2,2,0,0,0,0,0,0}, /* ST_ALANTE */
 {0,0,2,2,2,2,2,0,0,0,0,0,0}, /* ST_ATRAS */
 {0,0,0,0,0,0,0,0,0,0,0,0,0}, /* ST_BLOQUEA */
 {0,0,0,0,0,0,0,0,0,0,0,0,0}, /* ST_GOLPEA */
 {0,0,0,0,0,0,0,0,0,0,0,0,0}  /* ST_MUERTO */
}; 

CFGame::CFGame()
{}

CFGame::~CFGame()
{}

void CFGame::draw(SDL_Surface *surf)
{

    SDL_Rect vida1,vida2;

    vida1.x = 10;
    vida1.y = vida2.y = 10;

    vida1.w = player1->energia*3;
    vida2.w = player2->energia*3;

    vida1.h = vida2.h = 25;
    vida2.x = 790-vida2.w;

    
    // Dibujamos el fondo
    fondo->draw(NULL,surf,NULL);
    // Dibujamos los personajes
    player1->draw(surf);
    player2->draw(surf);
    // Dibujamos los marcadores bara de vida dependiendo
    // de la energia de jugador1 y jugador2
    SDL_FillRect(surf,&vida1,SDL_MapRGB(surf->format,0xFF,0,0));
    SDL_FillRect(surf,&vida2,SDL_MapRGB(surf->format,0xFF,0,0));

    SDL_Flip(surf);
}

void CFGame::keyUp(const SDL_KeyboardEvent *key)
{
 //TODO: Hacer lo mismo para el player 2
    switch(key->keysym.sym)
    {
        case SDLK_a:
            if( player1->vx < 0 && (player1->estado == ST_ALANTE || player1->estado == ST_ATRAS) ){
                   player1->parar();
                   player1->vx = 0;
            }
            break;
        case SDLK_d:
            if( player1->vx > 0 && (player1->estado == ST_ALANTE || player1->estado == ST_ATRAS) ){
                   player1->vx = 0;
                   player1->parar();
            }
           break;
        
        case SDLK_LEFT:
            if( player2->vx < 0 && (player2->estado == ST_ALANTE || player2->estado == ST_ATRAS) ){
                   player2->parar();
                   player2->vx = 0;
            }
            break;
        case SDLK_RIGHT:
            if( player2->vx > 0 && (player2->estado == ST_ALANTE || player2->estado == ST_ATRAS) ){
                   player2->vx = 0;
                   player2->parar();
            }
           break;
    }
}

void CFGame::keyDown(const SDL_KeyboardEvent *key)
{
  
    if(estado == E_IN_GAME){
    // TODO: Hacer lo mismo para el player2
    switch(key->keysym.sym)
    {
        // Teclas del jugador 1
        case SDLK_w: 
            switch(player1->estado){
                case ST_DEPIE:
                case ST_ALANTE:
                case ST_ATRAS:
                    player1->salto();
                    mix->playFX("wav/soplo.wav");
                    player1->vy=-50;
                    break;
                }
            break;
        case SDLK_a:
            if(player1->mirror){
                player1->atras();
            }
            else{
                player1->alante();
            }
            player1->vx=-10;
            break;
        case SDLK_d:
            if(player1->mirror){
                player1->alante();
            }
            else{
                player1->atras();
            }
            player1->vx=10;
             break;
        case SDLK_n:
             mix->playFX("wav/soplo-grave.wav");
             player1->patada();
            break;
        case SDLK_m:
             mix->playFX("wav/soplo-med.wav");
             player1->punyo();
            break;
        case SDLK_b:
             mix->playFX("wav/soplo-med.wav");
             player1->especial1();
            break;
        
        // Teclas del jugador 2
        case SDLK_UP: 
            switch(player2->estado){
                case ST_DEPIE:
                case ST_ALANTE:
                case ST_ATRAS:
                    player2->salto();
                    mix->playFX("wav/soplo.wav");
                    player2->vy=-50;
                    break;
                }
            break;
        case SDLK_LEFT:
            if(player2->mirror){
                player2->atras();
            }
            else{
                player2->alante();
            }
            player2->vx=-10;
            break;
        case SDLK_RIGHT:
            if(player2->mirror){
                player2->alante();
            }
            else{
                player2->atras();
            }
            player2->vx=10;
             break;
        case SDLK_KP0:
             player2->patada();
             mix->playFX("wav/soplo-grave.wav");
            break;
        case SDLK_KP1:
             mix->playFX("wav/soplo-med.wav");
             player2->punyo();
            break;
        case SDLK_KP2:
             mix->playFX("wav/soplo-med.wav");
             player2->especial1();
            break;
    }
  }

    if(key->keysym.sym == SDLK_ESCAPE){
        SDL_Quit();
        exit(1);
    }
}

void CFGame::mouseMove(const SDL_MouseMotionEvent *motion)
{}

void CFGame::mouseButtonUp(const SDL_MouseButtonEvent *button)
{}

void CFGame::mouseButtonDown(const SDL_MouseButtonEvent *button)
{}

void CFGame::userEvent(const SDL_UserEvent *user)
{}

void CFGame::init()
{
    mix = Mixer::getInstance(); 
    mix->init();
    mix->loadFX("wav/soplo.wav");
    mix->loadFX("wav/soplo-grave.wav");
    mix->loadFX("wav/soplo-med.wav");
    mix->loadFX("wav/golpe.wav");
    mix->loadFX("wav/yoya.wav");

    fondo = new Image();
    fondo->load("imagenes/escenario.png");
    
    /* De momento fale */
    string p = "perso/";
    string p1 = p+PLAYER1;
    string p2 = p+PLAYER2;

    player1 = new CFJugador();
    player1->loadConf(p1.c_str());

    player2 = new CFJugador();
    player2->loadConf(p2.c_str());
    estado = E_INIT_ROUND;
}

void CFGame::update(unsigned int time_delta)
{
    
    player1->update(time_delta);
    player2->update(time_delta);

    if (player1->x > player2->x){
        player1->mirror=1;
        player2->mirror=0;
    }
    else{
        player1->mirror=0;
        player2->mirror=1;
    }
    
    SDL_Rect rec1 = player1->getFrameRect();
    SDL_Rect rec2 = player2->getFrameRect();
   
    switch(estado)
    {
        case E_INIT_ROUND:
            player1->x = 400;
            player2->x = 100;
            
            player1->y = 600-rec1.h-10;
            player2->y = 600-rec2.h-10;
            
            player2->mirror = 1;
            player1->mirror = 0;

            player2->estado = player1->estado = ST_DEPIE;
            player2->energia = player1->energia = 100;
            
            player2->vx = player1->vx = 0;
            player2->vy = player1->vy = 0;
            
            estado = E_IN_GAME;
            break;
        case E_IN_GAME: {
           
            if(player1->energia < 0){
                player1->morir();
                player2->ganar();
                estado = E_FINISH;
            }
            if(player2->energia < 0){
                player2->morir();
                player1->ganar();
                estado = E_FINISH;
            }



            // Jugador 1
            switch(player1->estado){
                case ST_SALTO:
                case ST_SPATA:
                case ST_SPUNYO:
                    if(player1->y < 300){
                        player1->vy += 5;
                    } else {
                        player1->parar();
                        player1->vx = 0;
                        player1->vy = 0;
                        player1->y = 600-rec1.h-10;
                    }
                    break;
            }
            
            // Jugador 2
            switch(player2->estado){
                case ST_SALTO:
                case ST_SPATA:
                case ST_SPUNYO:
                    if(player2->y < 300){
                        player2->vy += 5;
                    } else {
                        player2->parar();
                        player2->vx = 0;
                        player2->vy = 0;
                        player2->y = 600-rec2.h-10;
                    }
                    break;
            }

            bool coll = false;
            SDL_Rect rcol;

            if(player1->mirror){
                Sprite *o = (Sprite *)player2;
                coll = player1->collideFlip(*o,&rcol);
                
            } else {
                Sprite *o = (Sprite *)player1;
                coll = player2->collideFlip(*o,&rcol);
            }
           
            if (coll){
                int ganador = coll_table[player1->estado][player2->estado];

                if (ganador != 0){

                    if(ganador==1){
                        mix->playFX("wav/yoya.wav");
                        player2->energia-=10;
                        player2->golpear();
                        
                   } else if (ganador==2) {
                       mix->playFX("wav/yoya.wav");
                       player1->energia-= 10;
                       player1->golpear(); ;
                   } else if (ganador==3) {
                    mix->playFX("wav/golpe.wav");
                   }
                }
            }
                        
            }
            break;
        case E_FINISH:
            player1->y = 600-rec1.h-10;
            player2->y = 600-rec2.h-10;
            player2->vx = player1->vx = 0;
            player2->vy = player1->vy = 0;
            break;
    }

}

void  CFGame::joyButtonDown(const SDL_JoyButtonEvent *jbutton) 
{}

void  CFGame::joyButtonUp(const SDL_JoyButtonEvent *jbutton) 
{}

void  CFGame::joyMotion(const SDL_JoyAxisEvent *axis) 
{}

bool CFGame::isEnd()
{
    return false;
}
/* ex: set cindent expandtab softtabstop=4 shiftwidth=4 tabstop=4: */
